using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Facebook.Service.Core;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using FriendsNearby.Services.Facebook;

namespace FriendsNearby.Services
{
    public class FriendsNearbyFacade : IServiceProvider
    {
        private ILocationService locationService;
        private IPeopleNearbyService peopleNearbyService;
        private ISocialNetworkService socialNetworkService;
        private IUsersService usersService;
        private IFacebookServiceFactory facebookServiceFactory;

        public FriendsNearbyFacade()
        {
            usersService = BuildUsersService();
            peopleNearbyService = BuildPeopleNearbyService();
            locationService = BuildLocationService(peopleNearbyService);
            socialNetworkService = BuildSocialNetworkService();
            facebookServiceFactory = BuildFacebookServiceFactory();
        }

        #region Factories
        protected virtual IUsersService BuildUsersService()
        {
            IUsersService usersService =  new UsersService(this);
            
            return PolicyInjection.Wrap<IUsersService>(usersService);
        }

        protected virtual ILocationService BuildLocationService(IPeopleNearbyService peopleNearbyService)
        {
            ILocationService locationService = new LocationService(peopleNearbyService);

            return PolicyInjection.Wrap<ILocationService>(locationService);
        }

        protected virtual IPeopleNearbyService BuildPeopleNearbyService()
        {

            IPeopleNearbyService peopleNearbyService = new PeopleNearbyService(this);

            return PolicyInjection.Wrap<IPeopleNearbyService>(peopleNearbyService);
        }

        protected virtual ISocialNetworkService BuildSocialNetworkService()
        {
            ISocialNetworkService socialNetworkService = new FacebookSocialNetworkService(this);

            return PolicyInjection.Wrap<ISocialNetworkService>(socialNetworkService);
        }

        protected virtual IFacebookServiceFactory BuildFacebookServiceFactory()
        {
            IFacebookServiceFactory facebookServiceFactory = new Facebook.FacebookServiceFactory();

            return PolicyInjection.Wrap<IFacebookServiceFactory>(facebookServiceFactory);
        }
        #endregion

        public ILocationService LocationService { get { return locationService; } }
        
        public IPeopleNearbyService PeopleNearbyService { get { return peopleNearbyService; } }
        
        public ISocialNetworkService SocialNetworkService  { get { return socialNetworkService; } }
        
        public IUsersService UsersService { get { return usersService; } }

        public FacebookSession FacebookSession
        {
            set { this.GetService<IFacebookServiceFactory>().RegisterKnownSession(value); }
        }
	        
        #region IServiceProvider Members

        public object GetService(Type serviceType)
        {
            if (serviceType.IsAssignableFrom(typeof(ILocationService)))
                return locationService;
            if (serviceType.IsAssignableFrom(typeof(IPeopleNearbyService)))
                return peopleNearbyService;
            if (serviceType.IsAssignableFrom(typeof(ISocialNetworkService)))
                return socialNetworkService;            
            if (serviceType.IsAssignableFrom(typeof(IUsersService)))
                return usersService;
            if (serviceType.IsAssignableFrom(typeof(IFacebookServiceFactory)))
                return facebookServiceFactory;
            return null;
        }

        #endregion
    }
}
